#include "notificationsclient.h"

NotifyNotification* m_notification = NULL;

//默认动作跳转（点击消息体跳转）和按钮跳转 demo
void DefaultAndButtonAction()
{
    NotifyNotification* notification;
    GError*             error = NULL;
    notify_init ("APPName");    //应用名字
    notification = notify_notification_new (gettext("Notify Summary ..."),   //主题
                                            gettext("Notify Body ..."),      //正文
                                            "NotifyIcon");                   //ICon
    m_notification = notification;
    //设置应用名字
    notify_notification_set_app_name          (notification,gettext("APPName"));
    //设置跳转动作（没有跳转动作可不设置）
    //注：notify_notification_add_action的2和4参数均是要执行的动作。区别是参数2的动作由通知中心执行；参数4的回调函数是应用自己执行。写一个就可以，动作负复杂的话建议使用参数4
    notify_notification_add_action (notification,
                                   "kylin-recorder",    //按钮执行的动作
                                   gettext("default"),  //默认动作（点击消息体时触发）
                                   (NotifyActionCallback)DefaultActionCallBack,  //默认动作回调函数
                                   NULL, NULL);
    notify_notification_add_action (notification,
                                   "ukui-control-center",
                                   gettext("控制面板"),  //按钮名字
                                   (NotifyActionCallback)Action1CallBack,  //按钮1的动作回调函数
                                   NULL, NULL);
    notify_notification_add_action (notification,
                                   "kylin-music",
                                   gettext("麒麟音乐"),  //按钮名字
                                   (NotifyActionCallback)Action2CallBack,  //按钮2的动作回调函数
                                   NULL, NULL);
    //设置驻留时间： -1：默认显示时间（由通知中心决定） 0：消息常驻  其他：显示相应时长(由应用决定，单位ms)
    notify_notification_set_timeout (notification, -1);
    //显示通知
    gboolean ret = notify_notification_show (notification, &error);
    if (!ret)
    {
        g_print ("Error is  \"%s\".\n", error->message);
        g_error_free (error);
    }
#if 0   //如果notify_notification_add_action 使用参数4时候需要这几行代码，不然回调函数无法执行
    GMainLoop* mainloop;
    mainloop=g_main_loop_new(NULL, FALSE);
    g_main_loop_run(mainloop);
    notify_uninit();
#endif
}
//链接跳转：例如截图应用，点击弹窗后会使用默认应用打开图片进行预览
void UrlAction()
{

    NotifyNotification* notification;
    GError*             error = NULL;
    notify_init ("APPName");    //应用名字
    //链接跳转正文格式为：<a href="跳转链接">要显示的文本内容</a>
    char *bodyUrl = "/usr/share/icons/ukui-icon-theme-fashion/scalable/apps/mate-about.svg";
    char *bodyText = "显示的正文内容： mate-about.svg";
    char bodyMsg[512] = "";
    strcat (bodyMsg, "<a href=\"");
    strcat (bodyMsg, bodyUrl);
    strcat (bodyMsg, "\">");
    strcat (bodyMsg, bodyText);
    strcat (bodyMsg, "</a>");
    g_print ("BodyMsg is  \"%s\".\n", bodyMsg);

    notification = notify_notification_new (gettext("Notify Summary ..."),   //主题
                                            bodyMsg,                         //正文
                                            "NotifyIcon");                   //Icon
    m_notification = notification;
    //设置应用名字
    notify_notification_set_app_name          (notification,gettext("APPName"));
    //设置驻留时间： -1：默认显示时间（由通知中心决定） 0：消息常驻  其他：显示相应时长(由应用决定，单位ms)
    notify_notification_set_timeout (notification, -1);
    //显示通知
    gboolean ret = notify_notification_show (notification, &error);
    if (!ret)
    {
        g_print ("Error is  \"%s\".\n", error->message);
        g_error_free (error);
    }
}
//关闭通知
void CloseNotify()
{
    GError*  error = NULL;
    gboolean ret = notify_notification_close(m_notification, &error);
    if (!ret)
    {
        g_print ("Error is  \"%s\".\n", error->message);
        g_error_free (error);
    }

}
//替换通知
void ReplaceNotify()
{
    GError*             error = NULL;
    gboolean ret = notify_notification_update (m_notification,
                                               gettext("Notify New Summary ..."),   //主题
                                               gettext("Notify New Body ..."),      //正文
                                               "NotifyIcon");                       //Icon
    if (!ret)
    {
        g_print ("Error is  \"%d\".\n", ret);
        g_error_free (error);
        return;
    }
    notify_notification_show (m_notification, &error);

}

void DefaultActionCallBack(NotifyNotification* notification,char *action, gpointer data)
{
    g_print("DefaultActionCallBack\n");
    return;
}
void Action1CallBack(NotifyNotification* notification,char *action, gpointer data)
{
    g_print("Action1CallBack\n");
    return;
}
void Action2CallBack(NotifyNotification* notification,char *action, gpointer data)
{
    g_print("Action2CallBack\n");
    return;
}


